// pages/create/create.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mode: 5,
    cash: 10,
    cash_input: '',
    show_cash: '10.56'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // const setting = wx.getStorageSync('setting');
    // if(setting){
    //   this.setData({
    //     mode: setting.mode,
    //     cash: setting.cash,
    //     show_cash: setting.cash > 0 ? Number(setting.cash * 1.006 + 0.5).toFixed(2) : '0.00'
    //   })
    // }

    this.setData({
      mode: app.mode || this.data.mode,
      cash: app.cash || this.data.cash,
      show_cash: this.calCash(app.cash || this.data.cash)
    })
  },

  onCashInput: function (e) {
    const cash = e.detail.value
    if (cash > 200) {
      this.setData({
        cash: 200,
        cash_input: 200,
        show_cash: this.calCash(200)
      })
    } else {
      this.setData({
        cash: cash,
        show_cash: this.calCash(cash)
      })
    }
  },

  onCashSelect: function (e) {
    const cash = e.currentTarget.dataset.cash
    this.setData({
      cash: cash,
      show_cash: this.calCash(cash),
      cash_input: ''
    })
  },

  onModeSelect: function (e) {
    const mode = e.currentTarget.dataset.mode
    this.setData({
      mode: mode
    })
  },

  onCreate: function (e) {
    var that = this
    if (this.clock == true) {
      return
    }
    that.clock = true
    wx.showLoading({
      title: '创建中',
    })

    app.request({
      url: app.root + 'round/create',
      method: 'post',
      data: {
        cash: this.data.cash,
        mode: this.data.mode
      },
      success: function (res) {
        if (res.data.err) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
          })
        } else {
          wx.reLaunch({
            url: '/pages/demo/demo?id=' + res.data.id,
            success: function (res) {
              wx.showModal({
                title: '提示',
                content: '创建成功，马上分享吧！',
              })
            }
          })
        }
        app.cash = that.data.cash
        app.mode = that.data.mode

        that.clock = false
        wx.hideLoading()
      }
    })
  },

  calCash: function(cash){
    return cash > 0 ? (parseInt(cash)+ .5).toFixed(2) : '0.00'
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})